package stmo.test1;

import org.junit.Test;


public class Test1 {

    /**
     *          快速排序算法
     */
    @Test
    public void test1() {
        int[] nums={7,9,4,3,8,7,22,19,46,2,10};
        int left = 0;
        int right = nums.length-1;
        qq(nums,left,right);
        for (int i=0;i<nums.length;i++) {
            System.out.println(nums[i]);
        }
    }

    private static void qq(int[] nums, int L, int R) {
        if ( L >= R) {
            return;
        }
        int left = L;
        int right = R;
        int pro = nums[left];
        while (left < right) {
            while (left < right && pro <= nums[right]) {
                right--;
            }
            if (left < right) {
                nums[left] = nums[right];
            }
            while (left < right && pro >= nums[left]) {
                left++;
            }
            if (left < right) {
                nums[right] = nums[left];
            }
            if (left >= right) {
                nums[left] = pro;
            }
        }
        qq(nums,L,right-1);
        qq(nums,left+1,R);
    }

    /**
     *    二分查找法
     */
    @Test
    public void test2() {
        //二分查找法
        int[] ss = new int[]{-55,-42,-36,-28,-19,-7,2,8,45,67,87,98,125};
        int yy = 98;                //被查找的数
        int head = 0;
        int end = ss.length-1;
        boolean isFast = true;
        while (head<=end) {
            int midd = (head+end)/2;
            if (yy == ss[midd]) {
                System.out.println("找到了：索引为"+midd);
                isFast = false;
                break;
            }else if (yy < ss[midd]) {
                end = midd-1;
            }else {
                head = midd+1;
            }
        }
        if (isFast) {
            System.out.println("没有找到");
        }
    }



}
